using AzrngTools.Utils;
using AzrngTools.Utils.Events;
using Common.Extension;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace AzrngTools.ViewModels.Format;

/// <summary>
/// json格式化
/// </summary>
public partial class JsonPageViewModel : ViewModelBase
{
    private readonly IMessageService _messageService;

    public JsonPageViewModel(IMessageService messageService)
    {
        _messageService = messageService;
    }

    #region 属性

    /// <summary>
    /// 原始文本
    /// </summary>
    [ObservableProperty]
    private string _original;

    #endregion

    /// <summary>
    /// 格式化json
    /// </summary>
    [RelayCommand]
    private void FormatJson()
    {
        try
        {
            if (Original.IsNullOrWhiteSpace())
            {
                _messageService.SendMessage("请输入要处理的内容");
                return;
            }

            Original = JsonHelper.JsonFormatter(Original);
        }
        catch (Exception e)
        {
            _messageService.SendMessage($"Json解析失败，请检查 ：{e.Message}");
        }
    }

    /// <summary>
    /// 压缩Json
    /// </summary>
    [RelayCommand]
    private void CompressJson()
    {
        try
        {
            if (Original.IsNullOrWhiteSpace())
            {
                _messageService.SendMessage("请输入要处理的内容");
                return;
            }

            Original = JsonHelper.JsonCompress(Original);
        }
        catch (Exception e)
        {
            _messageService.SendMessage($"Json解析失败，请检查 ：{e.Message}");
        }
    }
}